package task11;

import com.sun.org.apache.xpath.internal.SourceTree;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import javax.sound.midi.Soundbank;
import sun.awt.SunHints.Key;
import task8.HashSetTest;

/**
 * Create by IntelliJ IDEA
 *
 * @author: jsonor
 * @date-Time: 2018/4/27 8:40
 * @description:HashMap遍历练习
 */
public class Task11_2 {

  public static void main(String[] args) {
    //1、创建hasmap对象
    HashMap<String,Student> StudentHashMap=new HashMap<String,Student>();
    //2、添加元素
    Student zs=new Student("张三",18);
    Student ls=new Student("里斯",19);
    Student ww=new Student("王五",17);
    StudentHashMap.put("sl",zs);
    StudentHashMap.put("s2",ls);
    StudentHashMap.put("s3",ww);
    //3、遍历
    //通过键集合来获取值
    Iterator iterator=StudentHashMap.keySet().iterator();
    while (iterator.hasNext()){
      //每一个键
      String key=(String) iterator.next();
      //通过键得到值
      Student value=StudentHashMap.get(key);
      System.out.println(key+":"+value);
    }

    //通过键值对集合来获取键和值
    Iterator iterator1=StudentHashMap.entrySet().iterator();
    while (iterator.hasNext()){
      //每一个键值对
      Map.Entry<String,Student> entry= (Entry<String, Student>) iterator.next();
      //通过键值对获取键和值
      String key=entry.getKey();
      Student value=entry.getValue();
      System.out.println(key+"--"+value);
      }
  }

}
